import java.net.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.io.*;

public class OLD_FileServer
{
	static ServerSocket servsock;
	static Socket sock;
	static LinkedBlockingQueue<String> queueFiles = new LinkedBlockingQueue<String>();
	
	static QueueManager tQueueManager = new QueueManager(queueFiles);
	
	  public static void main(String[] args)
	  {
		  //tQueueManager.start();
		  queueFiles.add("/home/brian/woot.mp3");
		  try
		  {
			  // create socket
			  servsock = new ServerSocket(13267);
			  while (true)
			  {
				  System.out.println("Waiting...");
				  sock = servsock.accept();
				  System.out.println("Accepted connection : " + sock);
				  try {
					  System.out.println("Waiting to take a file off the queue");
					sendFile(queueFiles.take());
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				  
			  }
		  }
		  catch(IOException e)
		  {
			  e.printStackTrace();
		  }
	  }
	  
	  public static void sendFile(String fileName)
	  {
		  try {
		// sendfile	  
			  // TODO this has to be the fully qualified FILE PATH¬"!!!!
			  System.out.println("Trying to send shit");
		  File myFile = new File (fileName); // Get file
		  byte [] mybytearray  = new byte [(int)myFile.length()]; // Create byte array
		  FileInputStream fis;
		
			fis = new FileInputStream(myFile);
		 // Create file input stream
  
		  BufferedInputStream bis = new BufferedInputStream(fis); // Create buffer input stream from file input stream
		  bis.read(mybytearray,0,mybytearray.length);  // read bytes in from stream to array
  
		  ObjectOutputStream os = new ObjectOutputStream(sock.getOutputStream());  //create output stream
		  System.out.println("Sending...");
		  data myDataObject = new data(mybytearray);
		  os.writeObject(myDataObject);
		  //os.write(mybytearray,0,mybytearray.length);
		  os.flush();   //clear buffer
		  System.out.println("done sending?");
		  sock.close();  // close socket
		  } catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }
	  
	  
}

//Takes files off the queue and sends them to the appropriate handler function
class QueueManager implements Runnable
{
	private volatile Thread threadQueueManager;
	String fileName;
	LinkedBlockingQueue<String> queueFiles;
	
	QueueManager(LinkedBlockingQueue<String> qFiles)
	{
		queueFiles = qFiles;
	}
	
	void stop()
	{
		threadQueueManager = null;
	}
	
	void start()
	{
		threadQueueManager = new Thread(this);
		threadQueueManager.start();
	}
	
	public void run()
	{
		Thread thisThread = Thread.currentThread();
		System.out.println("Started the queue manager thread");
		while(threadQueueManager == thisThread)
		{
			try
			{
				System.out.println("Waiting to take a file off the queue");
				fileName = queueFiles.take();
				
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}
}